import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Trap here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public abstract class Trap extends Actor
{
    protected int damage;
    protected int delay;
    protected int initialDelay;
    protected int posX;
    protected boolean active;
    protected Timer activeTimer=new Timer();
    protected Timer timer=new Timer();
    
    public Trap(int x,int del,int iniD)
    {
        posX=x;
        delay=del;
        initialDelay=iniD;
        active=false;
    }
    
    public void act() 
    {
    }   
    
    public void activeImage()
    {
        active=true;
        GreenfootImage image=getImage();
        if(image.getTransparency()==0)
        {
            image.setTransparency(255);
            setImage(image);
        }
    }
    
    /*public boolean isTouchingGround()
    {
        return(getOneObjectAtOffset(0,0,Ground.class)!=null);
    }*/
    
    public Player getPlayer()
    {
        GameWorld world=(GameWorld)getWorld();
        Player player=world.getPlayer();
        return(player);
    }
    
    public CollisionBox getColPlayer()
    {
        GameWorld world=(GameWorld)getWorld();
        CollisionBox box=world.getColPlayer();
        return(box);
    }
    
    public boolean isTouchingPlayer()
    {
        if(!intersects(getColPlayer()))
        return(false);
        
        return(touch(getColPlayer()));
    }
    
    
    /*private boolean isVisuallyIntersecting(Actor actor)    
    {    
        // get width and height of images    
        int wA = getImage().getWidth();    
        int hA = getImage().getHeight();    
        int wB = actor.getImage().getWidth();    
        int hB = actor.getImage().getHeight();    
        // get world coordinates of origin point (0, 0) of images    
        int xAw = getX()-wA/2;    
        int yAw = getY()-hA/2;    
        int xBw = actor.getX()-wB/2;    
        int yBw = actor.getY()-hB/2;    
        // determine (x, y) on each image to begin checking from    
        // and dimensions of area to check    
        int xA = 0, yA = 0, xB = 0, yB = 0; // initialize start coord fields    
        int w = 0, h = 0; // initialize scan dimension fields    
        // determine values for fields just initialized    
        if (xAw > xBw)    
        {    
            xB = xAw-xBw;    
            if (xAw+wA > xBw+wB) w = wB-xB; else w = wA;    
        }    
        else    
        {    
            xA = xBw-xAw;    
            if (xBw+wB > xAw+wA) w = wA-xA; else w = wB;    
        }    
        if (yAw > yBw)    
        {    
            yB = yAw-yBw;    
            if (yAw+hA > yBw+hB) h = hB-yB; else h = hA;    
        }    
        else    
        {    
            yA = yBw-yAw;    
            if (yBw+hB > yAw+hA) h = hA-yA; else h = hB;    
        }    
        // perform checking    
        java.awt.Color trans = new java.awt.Color(0, 0, 0, 0);    
        for (int x=0; x<w; x++) for (int y=0; y<h; y++)    
            {    
                if (!trans.equals(getImage().getColorAt(xA+x, yA+y)) )//&&    
                    //!trans.equals(actor.getImage().getColorAt(xB+x, yB+y)))   
                    return true;  
        }    
        return false;    
    } */
 /**   
 * This is a perfect pixel-collision method.
 * 
 * @author (Busch2207 (Moritz L.)) 
 * @version (09.11.2013)
 * /
    
    /** This method is a pixel perfect collision detection. Returns true, if the object touchs the given Actor */
    public boolean touch(Actor a_big)
    {
        Actor a_small;
        if(getImage().getWidth()*getImage().getHeight()>a_big.getImage().getHeight()*a_big.getImage().getWidth())
        {
            a_small=a_big;
            a_big=this;
        }
        else
            a_small=this;

        int i_hypot=(int)Math.hypot(a_small.getImage().getWidth(),a_small.getImage().getHeight());

        GreenfootImage i=new GreenfootImage(i_hypot,i_hypot);
        i.drawImage(a_small.getImage(),i_hypot/2-a_small.getImage().getWidth()/2,i_hypot/2-a_small.getImage().getHeight()/2);
        i.rotate(a_small.getRotation());
        int w=i_hypot;

        GreenfootImage Ai = a_big.getImage(),
        i2=new GreenfootImage(i_hypot=(int)Math.hypot(Ai.getWidth(),Ai.getHeight()),i_hypot);
        i2.drawImage(Ai,i2.getWidth()/2-Ai.getWidth()/2,i2.getHeight()/2-Ai.getHeight()/2);
        i2.rotate(a_big.getRotation());
        Ai=i2;

        int
        x_Offset=a_big.getX()-a_small.getX()-(Ai.getWidth()/2-w/2),
        y_Offset=a_big.getY()-a_small.getY()-(Ai.getHeight()/2-w/2);

        boolean b = true;
        for(int yi =Math.max(0,y_Offset); yi<w && yi<i_hypot+y_Offset && b; yi++)
            for(int xi =Math.max(0,x_Offset); xi<w && xi<i_hypot+x_Offset && b; xi++)
                if(Ai.getColorAt(xi-x_Offset,yi-y_Offset).getAlpha()>0 )//&& i.getColorAt(xi,yi).getAlpha()>0)
                    b=false;
        return !b;
    }
}
        
